package com.woniuxy.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.nimbusds.jose.JOSEException;
import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * @author ：CuiYC
 * @description：
 * @date ：Created in 2025/3/10 16:20
 * @modified By：
 * 1.接收请求
 * 2.校验参数
 * 3.调service方法
 * 4.响应
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {

    private UserService userService;

    @PostMapping("/login")
    public ResponseData<?> login(@RequestBody User user){
        try {
            //账号或密码为空校验
            if(StrUtil.isBlank(user.getAccount()) || StrUtil.isBlank(user.getPassword())){
                return ResponseData.fail(ResponseEnum.PARAM_ERROR);
            }
            return userService.login(user);
        } catch (JOSEException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/addCart/{id}/{buyNum}")
    public ResponseData<?> addCart(@PathVariable("id") Integer id,
                                   @PathVariable("buyNum") Integer buyNum){
        try {
            return userService.addCart(id, buyNum);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/queryCartAndAddr")
    public ResponseData<?> queryUserCartAndAddr(){

        try {
            return userService.queryUserCartAndAddr();
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
